<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Config;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ConfigController extends CommonController
{
    //  get admin/config
	public function index()
	{
		$data = Config::orderBy('conf_order', 'asc')->get();
		foreach ($data as $key => $value) {
			switch ($value->field_type) {
				case 'input':
					$data[$key]['_html'] = '<input type="text" class="lg" name="conf_content[]" value="' . $value->conf_content .'">';
					break;
				case 'textarea':
					$data[$key]['_html'] = '<textarea class="lg" name="conf_content[]">' . $value->conf_content . '</textarea>';
					break;
				case 'radio':
					$array1 = explode(',', $value->field_value);	//$array1 = ['1|开启', '0|关闭']
					$str = '';
					foreach($array1 as $m) {
						// 1|开启
						$array2 = explode('|', $m);					//$array2 = ['1', '开启']或者['0', '关闭']
						$checked = $value->conf_content == $array2[0] ? 'checked':'';
						$str .= '<input type="radio" name="conf_content[]" value="'. $array2[0] .'" ' . $checked .'>'. $array2[1] . '&nbsp;&nbsp;&nbsp;&nbsp;';
					}
					$data[$key]['_html'] = $str;
					break;
			}
		}
		return view('admin.config.index', compact('data'));
	}

	//get admin/config/create
	public function create()
	{
		return view('admin.config.create');
	}

	//post admin/config  负责添加配置项的存储
	public function store()
	{
		$input = Input::except('_token');
		$rules = [
			'conf_name' => 'required',
			'conf_title' => 'required',
		];
		$message = [
			'conf_name.required' => '配置项名称名称不能为空',
			'conf_title.required' => '配置项标题不能为空',

		];
		$validator = Validator::make($input, $rules, $message);

		if ($validator->passes()) {
			$result = Config::create($input);
			if ($result) {
				return redirect('admin/config');
			} else {
				return back()->with('errors', '配置项添加失败，请稍后重试');
			}

		} else {
			return back()->withErrors($validator);
		}
	}

	//get admin/config/{config}/edit
	public function edit($conf_id)
	{
		$field = Config::find($conf_id);
		return view('admin.config.edit', compact('field'));
	}

	//put admin/config/{config}
	public function update($conf_id)
	{
		$input = Input::except('_token', '_method');
		$result = Config::where('conf_id', $conf_id)->update($input);
		if ($result) {
			$this->putFile();
			return redirect('admin/config');
		} else {
			return back()->with('errors', '配置项更新失败，请稍后重试');
		}
	}

	//delete admin/config/{config}
	public function destroy($conf_id)
	{
		$result = Config::where('conf_id', $conf_id)->delete();
		if ($result) {
			$this->putFile();
			$data = [
				'status' => 0,
				'msg' => '配置项删除成功'
			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '配置项删除失败'
			];
		}
		return $data;
	}

	public function changeOrder()
	{
		$input = Input::all();
		$link = Config::find($input['conf_id']);
		$link->conf_order = $input['conf_order'];
		$result = $link->update();
		if($result) {
			$data = [
				'status' => 0,
				'msg' => '配置项排序更新成功',
			];
		} else {
			$data = [
				'status' => 1,
				'msg' => '配置项排序更新失败，请稍后重试',
			];
		}
		return $data;
	}

	public function show()
	{

	}

	public function changeContent()
	{
		$input = Input::except('_token');
		foreach ($input['conf_id'] as $key => $value) {
			$result[] = Config::where('conf_id', $value)->update(['conf_content' => $input['conf_content'][$key]]);
		}
		$this->putFile();
		return back()->with('errors', '配置项更新成功');
	}

	public function putFile()
	{
		$config = Config::pluck('conf_content', 'conf_name')->all();
		$path = base_path() . '/config/web.php';
		$str = '<?php return ' . var_export($config, true) . ';';
		file_put_contents($path, $str);
	}
}
